angular.module('myApp')

    .controller('queryCtrl', ['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'myConstant', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                CacheService, myConstant) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //变量初始化
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {};

            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;
            vm.refresh = refresh;

            /*------------------以上配置通用---------------------*/



            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };
            /*------------------以上方法名可选择性通用---------------------*/


            //缓存数据初始化(需要缓存的key请自定义)


            CacheService.initCaches(vm.cached);


            function queryDetail() {
                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');

                //组织参数：查询条件 + 分页数据
                var postData = angular.copy(vm.queryBean);
                postData.pageSize = vm.pagination.pageSize;
                postData.pageNum = vm.pagination.pageNum;
                vm.pagination.queryBean = postData;
                $http.post(vm.url, vm.pagination, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination = data.object;
                            //清空选中记录
                            CheckboxService.clearChecked();
                        } else {
                            alert(data.msg);
                        }
                    });

            };

            function refresh(){
                if(window.confirm("确认重新请求清算协会获取数据字典？")){
                    $http.post(config.ctx + "/specially/dataDic/cache/refresh").then(
                        function(response) {
                            var data = response.data;
                            if (data.success) {
                                alert("操作成功");
                                queryDetail();
                            } else {
                                alert("操作失败");
                                queryDetail();
                            }
                        }, function(response) {
                            $log.debug("error");
                            queryDetail();
                        });
                }




            }


            function resetForm() {
                vm.queryBean = {};
            }

        }]);
